package com.lancoo.znbkxx.teacher.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.databinding.FmtCommentsDetailBinding
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel
import com.luck.picture.lib.entity.LocalMedia
import kotlinx.android.synthetic.main.fmt_publish_new.*

/**
 * @author : wealive
 * @date   : 2020/3/20 11:17
 * @desc   :
 */
class CommentDetailFragment : BaseVMFragment<HardSolutionViewModel, FmtCommentsDetailBinding>() {
    private val fmt by lazy { childFragmentManager.findFragmentByTag("publish") as PublishFragment }
    private val paperName by lazy { requireArguments().getString("paperName") }
    private val isOneOnOne by lazy { requireArguments().getBoolean("isOneOnOne") }
    val btnPublish by lazy { binding.btnPublish }
    val btnSave by lazy { binding.btnSave }
    private lateinit var ft: FragmentTransaction
    private var currentFragment = Fragment()
    private val publishFragment by lazy { PublishFragment.newInstance(paperName!!) }
    private val recordingFragment by lazy {
        RecordingFragment.newInstance(publishFragment.getAudioList())
    }

    companion object {
        private const val PAPERNAME = "paperName"
        private const val ISONEONONE = "isOneOnOne"
        fun newInstance(paperName: String, isOneOnOne: Boolean): CommentDetailFragment {
            val fragment = CommentDetailFragment()
            val bundle = Bundle()
            bundle.putBoolean(ISONEONONE, isOneOnOne)
            bundle.putString(PAPERNAME, paperName)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun providerVMClass(): Class<HardSolutionViewModel>? {
        return HardSolutionViewModel::class.java
    }

    override fun providerContext(): Any? {
        return activity
    }

    override fun setLayoutID(): Int {
        return R.layout.fmt_comments_detail
    }

    override fun initView() {
        ft = childFragmentManager.beginTransaction()
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        if (viewModel.hardSolution.value!!.State == 1) {
            btnSave.visibility = View.GONE
            btnPublish.text = "重新发布"
        }
        btnPublish.setOnClickListener {
            if (fmt.isBlank()) {
                MyToast.showToast("未制作任何内容，请勿发布")
                return@setOnClickListener
            }
            fmt.upLoad(1, isOneOnOne)
        }
        btnSave.setOnClickListener {
            if (fmt.isBlank()) {
                MyToast.showToast("未制作任何内容，请勿保存")
                return@setOnClickListener
            }
            fmt.upLoad(0, isOneOnOne)
        }
        when (val typeNo = viewModel.hardSolution.value!!.TkModel.TypeNo) {
            "A" -> ft.add(R.id.fl, SingleChoiceFragment.newInstance(typeNo)).commit()
            "b", "C", "d", "D", "f", "g", "H", "k", "m", "N", "P", "Q", "U", "V", "X" -> ft.add(
                    R.id.fl, ClozeSectionFragment.newInstance(typeNo)).commit()
            "E", "F", "G", "L", "S", "x" -> ft.add(R.id.fl,
                    ListeningFragment.newInstance(typeNo)).commit()
        }
//        val fragment = PublishFragment.newInstance(paperName!!)
//        ft.add(R.id.fl_publish, fragment, "publish").commit()
        switchFragment(publishFragment)
    }

    fun switchToRecording() {
        switchFragment(recordingFragment)
        binding.linearLayout2.visibility = View.GONE
    }

    fun saveRecord(type: Int, media: LocalMedia) {
        publishFragment.saveAudio(type, media)
    }

    @SuppressLint("SetTextI18n")
    fun switchToPublish(num: Int) {
        publishFragment.tv_to_hardsolution.text = "继续讲评(${num})"
        switchFragment(publishFragment)
        binding.linearLayout2.visibility = View.VISIBLE
    }

    private fun switchFragment(fragment: Fragment) {
        ft = childFragmentManager.beginTransaction()
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        if (currentFragment != fragment) {
            ft.hide(currentFragment)
            currentFragment = fragment
            if (!fragment.isAdded) {
                ft.add(R.id.fl_publish, fragment,
                        if (fragment is PublishFragment) "publish" else "recording"
                ).show(fragment).commit()
            } else {
                ft.show(fragment).commit()
            }
        } else {
            ft.show(currentFragment).commit()
        }
    }
}